package com.itouch.bracelet.diseaseinfo;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.itouch.bracelet.R;
import com.itouch.bracelet.activity.BaseActivity;
import com.itouch.bracelet.bean.DiseaseInfoDetail;
import com.itouch.bracelet.result.DiseaseInfoDetailArrayResult;
import com.itouch.util.JsonAndPhpUtil;

public class DiseaseInfoDetailListActivity extends BaseActivity implements View.OnClickListener{

	ListView listview;
	TextView tTitle;
	DiseaseInfoDetailListActivity mThis;
	ArrayList<DiseaseInfoDetail> mData;
	DiseaseInfoDetailAdapter mAdapter;
	int id = -1;
	TextView btn_chat;
	@Override
	protected int setContentViewById() {
		// TODO Auto-generated method stub
		return R.layout.diseaseinfo_detaillist;
	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		mThis = this;
		listview = (ListView) findViewById(R.id.list);
		tTitle = (TextView) findViewById(R.id.title);
//		Button btn_back = (Button) findViewById(R.id.back);
//		btn_back.setOnClickListener(this);
		btn_chat = (TextView) findViewById(R.id.chat);
		btn_chat.setVisibility(View.GONE);
		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mThis, DiseaseInfoDetailActivity.class);
				intent.putExtra("data", mData.get(arg2));
				startActivity(intent);
			}
		});
		
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		id = intent.getIntExtra("plateid", -1);
		if(id == -1)
		{
			Toast.makeText(mThis, "数据传输异常！", Toast.LENGTH_SHORT).show();
			return;
		}
		
		String title = intent.getStringExtra("plate");
		String label = intent.getStringExtra("label");
		tTitle.setText(title + " (" + label + ")");
		
		mAdapter = new DiseaseInfoDetailAdapter(mThis, null);
		listview.setAdapter(mAdapter);
		new GetDiseaseInfoDetailList().execute();
	}
	
	public class GetDiseaseInfoDetailList extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			show();
		}

		@Override
		protected String doInBackground(String... params) {
			List<NameValuePair> lists = new ArrayList<NameValuePair>();
			String url = Constants.URL + Constants.DISEASEINFO_DETAIL;
			lists.add(new BasicNameValuePair("plateId", id + ""));

			try {
				String ss = JsonAndPhpUtil.postHttpData(url, lists,
						mThis);
				Log.e("test", "ss_ss:" + ss);
				return ss;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			disMiss();
			if (result == null || result.length() < 1) {
				return;
			}
			DiseaseInfoDetailArrayResult a = null;
			try {
				a = new Gson().fromJson(result, DiseaseInfoDetailArrayResult.class);
			} catch (JsonSyntaxException e) {
				// TODO: handle exception
				Toast.makeText(mThis, "服务器数据异常！", Toast.LENGTH_SHORT).show();
			}

			if (a == null) {
				return;
			}
			if (a.isFlag()) {
				mData = a.getRows();
				mAdapter.refreshData(mData);
			} else {
				Toast.makeText(context, a.getMsg() + "", Toast.LENGTH_LONG)
						.show();
			}

		}

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
//		case R.id.back:
//			finish();
//			break;

		default:
			break;
		}
	}


}
